/*
 * Copyright (C) 2012 The Android Open Source Project, Futon Redemption
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.widgetpreview;

import android.app.Activity;
import android.appwidget.AppWidgetHost;
import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProviderInfo;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;

import java.io.File;

import com.futonredemption.android.widgetpreview.R;


public class WidgetPreviewActivity extends Activity {

    private static final String LOG_TAG = "WidgetPreviewActivity";
    private static final int APPWIDGET_HOST_ID = 2048;
    private static final int REQUEST_WIDGET = 0;
    private static final int REQUEST_CONFIGURE = 1;

    private ViewImageFactory mImageFactory = null;
    private AppWidgetHost mAppWidgetHost = null;
    private FrameLayout mAppWidgetFrame = null;
    private AppWidgetHostView mAppWidgetView = null;
    private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    private String mAppWidgetName;
    private int mPreviewWidth;
    private int mPreviewHeight;

    @Override
    public void onSaveInstanceState(Bundle outState) {
    	outState.putInt("appWidgetId", mAppWidgetId);
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAppWidgetFrame = (FrameLayout)findViewById(R.id.main_frame);

        mAppWidgetHost = new AppWidgetHost(getApplicationContext(), APPWIDGET_HOST_ID);

        final Object retainedObj = getLastNonConfigurationInstance();
        if (retainedObj instanceof AppWidgetProviderInfo) {
        	//restartActivity();
        	//finish();
        	
        	//AppWidgetProviderInfo info = (AppWidgetProviderInfo) retainedObj;
            int id = savedInstanceState.getInt("appWidgetId");
            //mAppWidgetHost.allocateAppWidgetId();
            setAppWidget(id);
        	/*
            AppWidgetProviderInfo info = (AppWidgetProviderInfo) retainedObj;
            int id = mAppWidgetHost.allocateAppWidgetId();
            
            AppWidgetManager.getInstance(getPreferredContext()).bindAppWidgetId(id, info.provider);
            setAppWidget(id);
            */
            
        } else if(savedInstanceState == null) { // Don't potentially recreate the selector activity.
        	
            startChooseActivity();
        }
    }
    
    @Override
    public void onStart() {
        super.onStart();
        mAppWidgetHost.startListening();
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        AppWidgetProviderInfo info = AppWidgetManager.getInstance(
        		getPreferredContext()).getAppWidgetInfo(mAppWidgetId);
        return info;
    }

    private void pickAppWidget() {
    	startChooseActivity();
    }
    
    private void startChooseActivity() {
        int id = mAppWidgetHost.allocateAppWidgetId();
        Intent selectIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
        selectIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
        startActivityForResult(selectIntent, REQUEST_WIDGET);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_WIDGET) {
            if (data != null) {
                int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
                if (data.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
                    appWidgetId = data.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
                }

                if (resultCode == RESULT_OK) {
                    setAppWidget(appWidgetId);
                } else {
                    mAppWidgetHost.deleteAppWidgetId(appWidgetId);
                    finish();
                }
            } else {
                finish();
            }
        } else if (requestCode == REQUEST_CONFIGURE) {
            if (data != null) {
                int appWidgetId = data.getExtras().getInt(
                        AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
                if (resultCode == RESULT_OK) {
                    finishSetAppWidget(appWidgetId);
                } else {
                    mAppWidgetHost.deleteAppWidgetId(appWidgetId);
                }
            }
        }
    }

    private Context getPreferredContext() {
    	return this.getApplicationContext();
    }
    private void setAppWidget(int appWidgetId) {
        if (mAppWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
            mAppWidgetHost.deleteAppWidgetId(mAppWidgetId);
        }

        /* Check for configuration */
        AppWidgetProviderInfo providerInfo =
            AppWidgetManager.getInstance(getPreferredContext()).getAppWidgetInfo(appWidgetId);

        if (providerInfo.configure != null) {
            Intent configureIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
            configureIntent.setComponent(providerInfo.configure);
            configureIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

            if (configureIntent != null) {
                try {
                    startActivityForResult(configureIntent, REQUEST_CONFIGURE);
                } catch (ActivityNotFoundException e) {
                    Log.d(LOG_TAG, "Configuration activity not found: " + e);
                    Toast errorToast = Toast.makeText(
                    		getPreferredContext(), R.string.configure_error, Toast.LENGTH_SHORT);
                    errorToast.show();
                }
            }
        } else {
            finishSetAppWidget(appWidgetId);
        }
    }

    private void finishSetAppWidget(int appWidgetId) {
        AppWidgetProviderInfo providerInfo =
            AppWidgetManager.getInstance(getPreferredContext()).getAppWidgetInfo(appWidgetId);
        if (providerInfo != null) {
            mAppWidgetView =
                    mAppWidgetHost.createView(getPreferredContext(), appWidgetId, providerInfo);

            mImageFactory = new ViewImageFactory(mAppWidgetView);
            
            int [] dimensions =
                    getLauncherCellDimensions(providerInfo.minWidth, providerInfo.minHeight);

            mPreviewWidth = dimensions[0];
            mPreviewHeight = dimensions[1];

            mAppWidgetName =
                AppWidgetManager.getInstance(getPreferredContext()).getAppWidgetInfo(appWidgetId).label;
            mAppWidgetName = mAppWidgetName.replaceAll("[^a-zA-Z0-9]", "_");

            ViewGroup.LayoutParams p = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            mAppWidgetView.setLayoutParams(p);
            mAppWidgetFrame.removeAllViews();
            mAppWidgetHost.deleteAppWidgetId(mAppWidgetId);
            mAppWidgetFrame.addView(mAppWidgetView, mPreviewWidth, mPreviewHeight);
            mAppWidgetId = appWidgetId;
        }
    }

    // Taken from CellLayout.java
    public int[] getLauncherCellDimensions(int width, int height) {
        // Always assume we're working with the smallest span to make sure we
        // reserve enough space in both orientations.
        Resources resources = getResources();
        int cellWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
        int cellHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
        int widthGap = resources.getDimensionPixelSize(R.dimen.workspace_width_gap);
        int heightGap = resources.getDimensionPixelSize(R.dimen.workspace_height_gap);
        int previewCellSize = resources.getDimensionPixelSize(R.dimen.preview_cell_size);

        // This logic imitates Launcher's CellLayout.rectToCell.
        // Always round up to next largest cell
        int smallerSize = Math.min(cellWidth, cellHeight);
        int spanX = (width + smallerSize) / smallerSize;
        int spanY = (height + smallerSize) / smallerSize;

        // We use a fixed preview cell size so that you get the same preview image for
        // the same cell-sized widgets across all devices
        width = spanX * previewCellSize + ((spanX - 1) * widthGap);
        height = spanY * previewCellSize + ((spanY - 1) * heightGap);
        return new int[] { width, height };
    }
    
    @Override
    public void onBackPressed() {
        if (!getFragmentManager().popBackStackImmediate()) {
            startChooseActivity();
        }
    }

    
    public void saveSnapshot() {
    	int textId = R.string.saving_preview;

        Toast preToast = Toast.makeText(getPreferredContext(), textId, Toast.LENGTH_SHORT);
        preToast.show();

        boolean success = mImageFactory.save(mAppWidgetName);
        
        if (success) {
            textId = R.string.preview_saved;
        } else {
            textId = R.string.preview_save_error;
        }

        Toast postToast = Toast.makeText(getPreferredContext(), textId, Toast.LENGTH_LONG);
        postToast.show();
    }
    
    public void shareSnapshot() {
    	File file = mImageFactory.buildFile(mAppWidgetName);
        if (file.exists()) {
            Intent emailIntent = new Intent(Intent.ACTION_SEND);
            emailIntent.setType("image/png");
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, mAppWidgetName + 
                    getResources().getString(R.string.email_subject));
            emailIntent.putExtra(Intent.EXTRA_TEXT,
                    getResources().getString(R.string.email_body));
            emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            startActivity(emailIntent);
        } else {
            Toast postToast = Toast.makeText(
            		getPreferredContext(), R.string.no_preview, Toast.LENGTH_SHORT);
            postToast.show();
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.actionbar_main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home: {
            	restartActivity();
                return true;
            }
           case R.id.menuSaveSnapshot: {
            	saveSnapshot();
            	return true;
            }
            case R.id.menuSendSnapshot: {
            	shareSnapshot();
            	return true;
            }
            case R.id.menuPickAppWidget: {
            	pickAppWidget();
            	return true;
            }
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    public void restartActivity() {

        // app icon in action bar clicked; go home
        Intent intent = new Intent(this, WidgetPreviewActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
}
